// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Windows.Forms;
using svs.Mobile.BeyondMedia.Objects;

namespace svs.Mobile.BeyondMedia
{
	public partial class ShortcutEditor : Form
	{
		private MenuItem Cancel = new MenuItem();

		public ShortcutEditor()
		{
			InitializeComponent();

			Cancel.Text = "Cancel";
			Cancel.Click += Cancel_Click;
		}

		private void Done_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void Clear_Click(object sender, EventArgs e)
		{
			int index = EditorList.SelectedIndices[0];
			Shortcut s = EditorList.Items[index].Tag as Shortcut;
			ShortcutKeyManager.ClearShortcutKey(s);
			ReloadKeys();
		}

		private void Assign_Click(object sender, EventArgs e)
		{
			TogglewAssigmentWindow(true);
		}

		private void OnLoad(object sender, EventArgs e)
		{
			Reload();
		}

		private void Reload()
		{
			EditorList.Items.Clear();

			foreach (Shortcut h in ShortcutKeyManager.Shortcuts)
			{
				if (h.IsSystemKey) // skip the system keys
					continue;
				
				string[] items = new string[2];
				items[0] = h.CommandName;
				items[1] = h.KeyName;
				ListViewItem lvi = EditorList.Items.Add(new ListViewItem(items));
				lvi.Tag = h;
			}

			if (EditorList.Items.Count > 0)
				EditorList.Items[0].Selected = true;
		}

		private void ReloadKeys()
		{
			foreach (ListViewItem lvi in EditorList.Items)
			{
				Shortcut s = lvi.Tag as Shortcut;
				lvi.SubItems[1].Text = s.KeyName;
			}
		}

		private void OnKeyDown(object sender, KeyEventArgs e)
		{
			if (AssignWindow.Visible)
			{
				TogglewAssigmentWindow(false);

				if ((e.KeyCode == Keys.F1) || (e.KeyCode == Keys.F2))
				{
					;
				}
				else
				{
					int index = EditorList.SelectedIndices[0];
					Shortcut s = EditorList.Items[index].Tag as Shortcut;
					ShortcutKeyManager.SetShortcutKey(s, e.KeyCode);
					ReloadKeys();
					Helpers.Beep();
					e.Handled = true;
				}
			}
			else
			{
				if(e.KeyCode == Keys.Enter)
				{
					TogglewAssigmentWindow(true);
				}
			}
		}

		private void OnResize(object sender, EventArgs e)
		{
			EditorList.Columns[1].Width = 120;
			if (EditorList.Width > 240)
			{
				EditorList.Columns[0].Width = EditorList.Width - (EditorList.Columns[1].Width + 15);
			}
			else
				EditorList.Columns[0].Width = 120;
		}

		private void TogglewAssigmentWindow(bool i_show)
		{
			ShortCutMenu.MenuItems.Clear();

			if (i_show)
			{
				AssignWindowLabel.Visible = AssignWindow.Visible = true;
				ShortCutMenu.MenuItems.Add(Cancel);
			}
			else
			{
				AssignWindowLabel.Visible = AssignWindow.Visible = false;
				ShortCutMenu.MenuItems.Add(Done);
				ShortCutMenu.MenuItems.Add(menu);
			}
		}

		private void Cancel_Click(object sender, EventArgs e)
		{
			if (AssignWindow.Visible)
				TogglewAssigmentWindow(false);
		}
	}
}